/*
 *@description:权限管理
 *@Author:@reynold
 *@Date:2025-03-22 11:06:58
 *@Update:2025-03-22 11:06:58
 *@Version: V1.0.0
*/
<template>
  <Card>
    <div class="auth_box">
      <div class="header">
        <div class="btn">
          <el-button size="mini" type="primary" @click="showAdd">添加</el-button>
        </div>
      </div>
      <div class="content">
        <el-table
          :data="tableData"
          size="small"
          row-key="id"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          style="width: 100%">
          <el-table-column
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="路径"
            prop="route">
          </el-table-column>
          <el-table-column
            label="类型"
            prop="type">
          </el-table-column>
          <el-table-column
            label="编号"
            prop="num">
          </el-table-column>
          <el-table-column
            label="状态">
            <template slot-scope="scope">
              <el-tag type="success" size="mini" effect="dark" v-if="scope.row.status == '显示'">{{ scope.row.status }}</el-tag>
              <el-tag type="warning" size="mini" effect="dark" v-else>{{ scope.row.status }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="100">
            <template slot-scope="scope">
              <el-button class="columnBtn1" type="text" size="small" @click="del(scope.row)">删除</el-button>
              <el-button class="columnBtn2" type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- dialog -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="名称">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="路径">
            <el-input v-model="form.route" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-input v-model="form.type" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="活动状态区域">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option label="显示" value="显示"></el-option>
              <el-option label="隐藏" value="隐藏"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addauth">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </Card>
</template>

<script>
import Card from '@/components/Card/index.vue'
import instance from '@/utils/instance';
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: '权限1', route: '/permission1', type: '菜单', num: 1002,  status: '显示' },
        { id: 2, name: '权限2', route: '/permission2', type: '按钮', num: 2002,  status: '隐藏' },
        { id: 3, name: '权限3', route: '/permission3', type: '菜单', num: 3002,  status: '显示' },
        { id: 4, name: '权限4', route: '/permission4', type: '按钮', num: 4002,  status: '隐藏' },
        { id: 5, name: '权限5', route: '/permission5', type: '菜单', num: 5002,  status: '显示' },
        { id: 6, name: '权限6', route: '/permission6', type: '按钮', num: 6002,  status: '隐藏' },
        { id: 7, name: '权限7', route: '/permission7', type: '菜单', num: 7002,  status: '显示' },
        { id: 8, name: '权限8', route: '/permission8', type: '按钮', num: 8002,  status: '隐藏' }
      ],
      title: '',
      form: {},
      dialogFormVisible: false
    }
  },
  methods: {
    getlist() {
      instance.get('/menus/list').then(res => {
        if (res.data.data && res.data.data.length > 0) {
          this.tableData = res.data.data;
        }
      }).catch(error => {
        console.error('获取数据失败:', error);
      });
    },
    // 删除
    del(scope) {
      if (confirm('确定要删除这条权限吗？')) {
        instance.delete(`/menus/delete/${scope.id}`).then(res => {
          console.log(res);
          this.getlist();
        }).catch(error => {
          console.error('删除失败:', error);
        });
      }
    },
    // 编辑
    edit(scope) {
      this.title = '编辑权限';
      this.dialogFormVisible = true;
      this.form = { ...scope };
    },
    addauth() {
      instance.post('/menus/add', this.form).then(res => {
        console.log(res);
        this.getlist();
        this.dialogFormVisible = false;
      }).catch(error => {
        console.error('添加失败:', error);
      });
    },
    // 注意：用户是不会去操作权限的，添加了也没有对应的路由规则
    showAdd() {
      this.title = '添加权限';
      this.dialogFormVisible = true;
      this.form = {};
    }
  },
  created() {
    this.getlist();
  },
  components: {
    Card
  }
}
</script>

<style lang="less" scoped>
.card_box[data-v-a53d947a] {
  overflow: hidden;
}
</style>    